#ifndef QTSERIOVYPORT_H
#define QTSERIOVYPORT_H

#include <QObject>
#include <QTimer>
#include <QString>


#ifdef Q_OS_UNIX
/********************************/
/*      linuxovy knihovny       */
/********************************/
#include <stdio.h> // standard input / output functions
#include <string.h> // string function definitions
#include <unistd.h> // UNIX standard function definitions
#include <fcntl.h> // File control definitions
#include <errno.h> // Error number definitions
#include <termios.h> // POSIX terminal control definitionss
#include <time.h>   // time calls
#elif (defined Q_OS_WIN)
/********************************/
/*      windowsovy knihovny     */
/********************************/
#include <windows.h>
#endif


typedef enum{
#ifdef Q_OS_UNIX
    //LINUX
//    QT_BAUD_50 = B50,
//    QT_BAUD_75 = B75,
    QT_BAUD_110 = B110,
//    QT_BAUD_134 = B134,
//    QT_BAUD_150 = B150,
//    QT_BAUD_200 = B200,
    QT_BAUD_300 = B300,
    QT_BAUD_600 = B600,
    QT_BAUD_1200 = B1200,
//    QT_BAUD_1800 = B1800,
    QT_BAUD_2400 = B2400,
    QT_BAUD_4800 = B4800,
    QT_BAUD_9600 = B9600,
    QT_BAUD_19200 = B19200,
    QT_BAUD_38400 = B38400,
    QT_BAUD_57600 = B57600,
    QT_BAUD_115200 = B115200,
//    QT_BAUD_230400 = B230400,
//    QT_BAUD_460800 = B460800,
//    QT_BAUD_500000 = B500000,
//    QT_BAUD_576000 = B576000,
//    QT_BAUD_921600 = B921600,
//    QT_BAUD_1000000 = B1000000,
//    QT_BAUD_1152000 = B1152000,
//    QT_BAUD_1500000 = B1500000,
//    QT_BAUD_2000000 = B2000000,
//    QT_BAUD_2500000 = B2500000,
//    QT_BAUD_3000000 = B3000000,
//    QT_BAUD_3500000 = B3500000,
//    QT_BAUD_4000000 = B4000000,
#elif (defined Q_OS_WIN)
    //WINDOWS
    QT_BAUD_110 = CBR_110,
    QT_BAUD_300 = CBR_300,
    QT_BAUD_600 = CBR_600,
    QT_BAUD_1200 = CBR_1200,
    QT_BAUD_2400 = CBR_2400,
    QT_BAUD_4800 = CBR_4800,
    QT_BAUD_9600 = CBR_9600,
//    QT_BAUD_14400 = CBR_14400,
    QT_BAUD_19200 = CBR_19200,
    QT_BAUD_38400 = CBR_38400,
//    QT_BAUD_56000 = CBR_56000,
    QT_BAUD_57600 = CBR_57600,
    QT_BAUD_115200 = CBR_115200,
//    QT_BAUD_128000 = CBR_128000,
//    QT_BAUD_256000 = CBR_256000
#endif
}QT_SERIAL_BAUD;

typedef enum{
#ifdef Q_OS_UNIX
    //LINUX

#elif (defined Q_OS_WIN)
    //WINDOWS
    QT_ONE_STOPBIT = STOPBITS_10,
    QT_TWO_STOPBIT = STOPBITS_20,
    QT_ONE_FIVE_STOPBITS = STOPBITS_15
#endif
}QT_SERIAL_STOPBITS;


typedef enum{
#ifdef Q_OS_UNIX
    //LINUX
#elif (defined Q_OS_WIN)
    //WINDOWS
    QT_NON_PARITY = 'n',
    QT_EVEN_PARITY = 'e',
    QT_ODD_PRITY = 'o'
#endif
}QT_SERIAL_PARITY;

typedef enum{
    ER_ZADNA_CHYBA = 0,
    ER_OTEVRENI_PORTU,
    ER_ZISKANI_NASTAVENI_PORTU,
    ER_NASTEVENI_PORTU,
    ER_NASTAVENI_TIMEOUT,
    ER_CHYBA_CTENI,
    ER_UZAVRENI,
    ER_CHYBI_NAZEV_PORTU,
    ER_NASTAVENI_BUFFERU,
    ER_CHYBA_ZAPISU
}QT_SERIAL_ERROR;

typedef struct{
    QString name;
    QT_SERIAL_BAUD baud;
    QT_SERIAL_PARITY parity;
    char dataSize;
    QT_SERIAL_STOPBITS stopBits;
}QtSerialConfiguration;


class QtSeriovyPort : public QObject
{
    Q_OBJECT
public:
    explicit QtSeriovyPort(QObject *parent = 0);
    virtual ~QtSeriovyPort();

    virtual int openPort(QString name);
    virtual int openPort(QString name, QT_SERIAL_BAUD baud, char dataSize, QT_SERIAL_STOPBITS stopBits, QT_SERIAL_PARITY parity);
    virtual int openPort(QtSerialConfiguration cfg);

    virtual int configurePort(QT_SERIAL_BAUD baud, char dataSize, QT_SERIAL_STOPBITS stopBits, QT_SERIAL_PARITY parity);
    virtual int configurePort(QtSerialConfiguration cfg);


    virtual QtSerialConfiguration   getConfiguraion();
    virtual QString                 getPortName();
    virtual QT_SERIAL_BAUD          getBaud();
    virtual char                    getDataSize();
    virtual QT_SERIAL_STOPBITS      getStopBits();
    virtual QT_SERIAL_PARITY        getParity();



    virtual int read(QString *str);
    virtual int read(char *c);
    virtual int read(int *i);
    virtual int read(long *l);

    virtual int write(QString *str);
    virtual int write(char *c);
    virtual int write(int *i);
    virtual int write(long *l);

    virtual int close();

private:

    virtual int openPort();
    virtual int configurePort();

    QtSerialConfiguration *configuration;
    QT_SERIAL_ERROR chyba;


#ifdef Q_OS_UNIX
    //LINUX
    int port; // file description for the serial port
    struct termios port_settings;      // structure to store the port settings in
#elif (defined Q_OS_WIN)
    //WINDOWS
    HANDLE  port;
    DCB portSettings;
    COMMTIMEOUTS timeouts;
#endif
};

#endif // QTSERIOVYPORT_H
